package net.atomarrow.bs.controller;

import net.atomarrow.annotation.MethodUrl;
import net.atomarrow.bean.ServiceResult;
import net.atomarrow.bean.UploadFile;
import net.atomarrow.bs.domain.TbTopic;
import net.atomarrow.bs.form.TopicBsForm;
import net.atomarrow.bs.service.TopicBsService;
import net.atomarrow.render.Render;
import net.atomarrow.sy.base.BaseController;
import net.atomarrow.sy.staticvariable.FM;
import net.atomarrow.sy.staticvariable.PageSize;
import net.atomarrow.util.FileUtil;
public class TopicController extends BaseController {

    private TopicBsService topicBsService;
    
    public Render index(){
        return renderJsp("index");
    }
    public Render list(){
        TopicBsForm form =getParamObject("form", TopicBsForm.class);
        setAttr("topics", topicBsService.getList(form,checkPager(PageSize.PAGE_SIZE_PC)));
        setAttr("form", form);
        return renderJsp("/topic/listTopic");
    }
    
    public Render initAdd(){
        return renderJsp("addTopic");
    }
    
    public Render initModify(){
        setAttr("topic",topicBsService.getById(TbTopic.class,getParamInteger("id")));
        return renderJsp("/topic/modifyTopic");
    }
    
    public Render add(){
        return renderJsonServiceResult(topicBsService.addTopic(getParamObject("topic", TbTopic.class)));
    }
    
    public Render modify(){
        return renderJsonServiceResult(topicBsService.modifyTopic(getParamObject("topic", TbTopic.class)));
    }
    
    public Render del(){
        return renderJsonServiceResult(topicBsService.delById(getParamInteger("id")));
    }
    
    public Render addByExcel(){
        UploadFile file=getFile("upload");
        if(!FileUtil.checkSuffix(file.getFileName(),"xls")){
             return renderJsonFail(FM.UPLOAD_FILE_FORMAT_ERROR);
        }
        ServiceResult result=topicBsService.addFromExcel(file.getFilePath());
        return renderJsonServiceResult(result);
    }

    public Render downloadModel(){
        return renderFile("topic.xls", topicBsService.getExcelModel(getRealPath("/download/topic.xls")));
    }
    
    public Render export(){
    	return renderFile("topic.xls", topicBsService.export(getParamObject("form", TopicBsForm.class)));
    }
//    @Url("/")
    @MethodUrl("def")
    public Render abc(){
        return renderFile("topic.xls", topicBsService.export(getParamObject("form", TopicBsForm.class)));
    }
    
    
    public Render testParam(){
    	String p1 = getParamString(0);
    	Integer p2 = getParamInteger(1);
    	
    	StringBuilder sb = new StringBuilder();
    	sb.append("第1个参数：");
    	sb.append(p1);
    	sb.append("第2个参数：");
    	sb.append(p2);
    	return renderJson(sb.toString());
    }
    
    
    public Render testExc(){
    	Integer a=null;
//    	a++;
    	topicBsService.addExc();
    	
//    	throw new RuntimeException("a");
    	return renderJsonSuccess();
    }
    
}
